<?php
set_time_limit(300);
defined('IN_PHPCMS') or exit('No permission resources.');
//模型缓存路径
define('CACHE_MODEL_PATH',CACHE_PATH.'caches_model'.DIRECTORY_SEPARATOR.'caches_data'.DIRECTORY_SEPARATOR);
//定义在单独操作内容的时候，同时更新相关栏目页面
define('RELATION_HTML',true);

pc_base::load_app_class('admin','admin',0);
pc_base::load_sys_class('form','',0);
pc_base::load_app_func('util');
pc_base::load_sys_class('format','',0);

/**
 * 导师管理
 */
class mentor extends admin
{
	private $db;
	public $siteid;

	public function __construct()
	{
		parent::__construct();
		$this->db = pc_base::load_model('mentor_model');
		$this->siteid = get_siteid();
	}

	/**
	 * 导师列表
	 */
	public function index ()
	{
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$where = ' status = 1 AND siteid = ' . $this->siteid . ' ';
		if ($_GET['name']) {
			$name = $_GET['name'];
			$where .= "AND `name` LIKE '%$name%' ";		
		}
		$industrial_list = $this->db->listinfo($where, 'id DESC', $page, 15);
		$pages = $this->db->pages;
		pc_base::load_sys_class('format', '', 0);
		include $this->admin_tpl('mentor_list');
	}

	/**
	 * 添加导师
	 */
	public function add ()
	{
		if (isset($_POST['dosubmit']) && !empty($_POST['dosubmit'])) {
			$mentor = $this->check($_POST['mentor']);
			$id = $this->db->insert($mentor, true);
			if ($id) {
				showmessage(L('add_industrial_success'), '?m=mentor&c=mentor&a=index');
			} else {
				showmessage(L('add_industrial_error'), HTTP_REFERER);
			}	
		} else {
			//获取站点模板信息
			$show_dialog = $show_validator = true;
			pc_base::load_app_func('global', 'admin');
			include $this->admin_tpl('mentor_add');
		}
	}

	/**
	 * 导师修改
	 */
	public function edit (){
		if (!isset($_GET['id']) || empty($_GET['id'])) {
			showmessage(L('illegal_action'), HTTP_REFERER);
		}
		$id = intval($_GET['id']);
		if (isset($_POST['dosubmit']) && !empty($_POST['dosubmit'])) {
			$mentor = $this->check($_POST['mentor'], 'edit');
			$id = $this->db->update($mentor, array('id'=>$id, 'siteid'=>$this->siteid));
			if ($id) {
				showmessage(L('edit_industrial_success'), '?m=mentor&c=mentor&a=index');
			} else {
				showmessage(L('edit_industrial_error'), HTTP_REFERER);
			}	
		} else {
			$show_dialog = $show_validator = true;
			$info = $this->db->get_one(array('id'=>$id, 'siteid'=>$this->siteid));
			extract($info);
			//获取站点模板信息
			pc_base::load_app_func('global', 'admin');
			include $this->admin_tpl('mentor_edit');
		}
	}

	/**
	 * 导师删除
	 */
	public function delete ()
	{
		if((!isset($_GET['id']) || empty($_GET['id'])) && (!isset($_POST['id']) || empty($_POST['id']))) {
			showmessage(L('illegal_parameters'), HTTP_REFERER);
		} else {
			if(is_array($_POST['id'])){
				foreach($_POST['id'] as $jobid_arr) {
					$this->db->update(array('status'=>'-1'),array('id'=>$jobid_arr));
				}
				showmessage(L('operation_success'),HTTP_REFERER);
			}else{
				$id = intval($_GET['id']);
				if($id < 1) return false;
				$result = $this->db->update(array('status'=>'-1'),array('id'=>$id));
				if($result){
					showmessage(L('operation_success'),HTTP_REFERER);
				}else {
					showmessage(L("operation_failure"),HTTP_REFERER);
				}
			}
		}
	}

	/**
	 * ajax检验导师是否存在，避免导师重复
	 */
	public function public_check_mentor ()
	{
		if(!$_GET['name']) {echo 0;die;}
		if(pc_base::load_config('system', 'charset')=='gbk') {
			$_GET['name'] = safe_replace(iconv('UTF-8', 'GBK', $_GET['name']));
		}
		if($_GET['id']) {
			$id = intval($_GET['id']);
			$r = $this->db->get_one(array('id'=>$id, 'siteid' => $this->siteid));
			if($r['name'] == $_GET['name']) {
				echo 1;die;
			}
		}
		$r = $this->db->get_one(array('siteid' => $this->siteid, 'name' => $_GET['name']), 'id');
		if($r['id']) {
			echo 0;die;
		} else {
			echo 1;die;
		}
	}

	/**
	 * 检验导师是否存在，避免导师重复
	 */
	public function public_check_mentors ($name,$id)
	{
		if(!$name) {return 0;}
		if(pc_base::load_config('system', 'charset')=='gbk') {
			$name = safe_replace(iconv('UTF-8', 'GBK', $name));
		}
		if($id) {
			$r = $this->db->get_one(array('id'=>$id, 'siteid' => $this->siteid));
			if($r['name'] == $name) {
				return 1;
			}
		}
		$r = $this->db->get_one(array('siteid' => $this->siteid, 'name' => $name), 'id');
		if($r['id']) {
			return 0;
		} else {
			return 1;
		}
	}

	/**
	 * 表单验证
	 * @param array $data 表单传递的值
	 * @param string $a add/edit添加操作时，自动加上默认值
	 */
	private function check($data, $a = 'add') {
		if(!$data['name']) showmessage(L('mentor_name').L('no_empty'), HTTP_REFERER);
		if(!$this->public_check_mentors($data['name'],$_GET['id'])) showmessage(L('mentor_exist'), HTTP_REFERER);
		if($a=='add') {
			$data['siteid'] = $this->siteid;
			$data['addtime'] = SYS_TIME;
			$data['addusername'] = param::get_cookie('admin_username');
			$data['adduserid'] = $_SESSION['userid'];
		} else {
			$data['updatetime'] = SYS_TIME;
		}
		return $data;
	}

	
}